
import { _decorator, Component, Node, SystemEvent, EventTouch,Touch } from 'cc';
import { GameManager } from '../framework/gameManager';
const { ccclass, property } = _decorator;

@ccclass('UiMain')
export class UiMain extends Component {
   
    @property
    public planeSpeed=1;
    @property(Node)
    public playerPlane:Node=null;

    @property(GameManager)
    public GameManager:GameManager=null;

    @property(Node)
    public gameStart:Node=null;
    @property(Node)
    public game:Node=null;
    @property(Node)
    public gaemover:Node=null;
    
    start () 
    {
       this.node.on(SystemEvent.EventType.TOUCH_START,this._touchStart,this);
       this.node.on(SystemEvent.EventType.TOUCH_MOVE,this._touchMove,this);
       this.node.on(SystemEvent.EventType.TOUCH_END,this._touchEnd,this);

       this.gameStart.active=true;

    }

    // update (deltaTime: number) {
    //     // [4]
    // }
     public reStart()
     {
       this.gaemover.active=false;
       this.game.active=true;
       this.GameManager.reStart();
       this.GameManager.playerAudioEffect('button');

     }

     public retrunMain()
     {
        this.gaemover.active=false;
        this.gameStart.active=true;
        this.GameManager.returnMain();
        this.GameManager.playerAudioEffect('button');
     }
    _touchStart(touch:Touch,event:EventTouch)
    {   
        if(this.GameManager.isGameStart)
        {
            this.GameManager.isShooting(true); 
        }else
        {
            this.gameStart.active=false;
            this.game.active=true;
            this.GameManager.gameStart();
            this.GameManager.playerAudioEffect('button');
        }
      
    }
    
    _touchMove(touch:Touch,event:EventTouch)
    {
        if(!this.GameManager.isGameStart)
        {
            return ;
        }
        const delta=touch.getDelta();  
        let pos=this.playerPlane.position;//获取当前节点位置
        this.playerPlane.setPosition(pos.x+0.01*this.planeSpeed*delta.x,pos.y,pos.z-0.01*this.planeSpeed*delta.y);
    }
  
    _touchEnd(touch:Touch,event:EventTouch)
    {
        if(!this.GameManager.isGameStart)
        {
            return ;
        }
        this.GameManager.isShooting(false);
    }

}

/**
 * [1] Class member could be defined like this.
 * [2] Use `property` decorator if your want the member to be serializable.
 * [3] Your initialization goes here.
 * [4] Your update function goes here.
 *
 * Learn more about scripting: https://docs.cocos.com/creator/3.0/manual/en/scripting/
 * Learn more about CCClass: https://docs.cocos.com/creator/3.0/manual/en/scripting/ccclass.html
 * Learn more about life-cycle callbacks: https://docs.cocos.com/creator/3.0/manual/en/scripting/life-cycle-callbacks.html
 */
